package sy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import sy.pageModel.BaseEntity;
import sy.pageModel.Member;
import sy.pageModel.OrderDoctor;
import sy.util.EMailUtil;
import sy.util.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016-07-08.
 */
public class BaseService {
    @Autowired
    protected JdbcTemplate template;

    protected void updateMenber(String cardId,String orderId,Double price,String status) throws Exception{
        if (status.equals(OrderDoctor.ORDER_STATUS_ID_SERVICING)) {
            final String[] cardType = {""}, sqkz = {""};
            final List<Member> memberList = new ArrayList<>();
            String sql = "select pid from view_hy v where vnumber=?";
            String pid = template.queryForObject(sql, new Object[]{cardId}, String.class);
            if (pid != null && !"".equals(pid.trim())) cardId = pid;//判断pid是否为空 不为空则改变卡号值
            sql = "select u.typeid,v.* from view_hy v,u_user u where u.userid=v.vnumber AND u.userid=?";
            template.query(sql, new Object[]{cardId}, new RowCallbackHandler() {
                @Override
                public void processRow(ResultSet rs) throws SQLException {
                    Member member = new Member();
                    member.setCardId(rs.getString("v.vnumber"));
                    member.setBalance(rs.getDouble("v.balance"));
                    member.setPassportId(rs.getString("u.typeid"));
                    member.setJifen(rs.getInt("jifen"));
                    memberList.add(member);
                }
            });
            Member member = memberList.get(0);
            if (member.getBalance() >= price) {
                //确认服务 修改会员余额
                if (member.getPassportId().equals(Member.MEMBER_CATEGORY_PAY))
                    sql = "update z_viphy_zk  SET xf_money=?,jifen=? where vnumber=?";
                if (member.getPassportId().equals(Member.MEMBER_CATEGORY_HONOR))
                    sql = "update z_ryhy_zk  SET num_money=?,jifen=? where vnumber=?";
                template.update(sql, new Object[]{(member.getBalance() - price),member.getJifen() + Math.floor(price/100), cardId});
                //添加收支表明细
                sql = "insert into xj_transaction_record(typeid,t_time,money,balance,order_number,vnumber) values(?,?,?,?,?,?)";
                Object[] paras = new Object[]{Member.MEMBER_PAY_MEMBERSHIP_FEES, StringUtil.getDate(), price, (member.getBalance() - price), orderId, member.getCardId()};
                template.update(sql, paras);
            } else {
                throw new RuntimeException("会员余额不足，请通知会员充值！");
            }
        }
    }

    protected String orderStatus(String st){
        String status="";
        if (st.equals(OrderDoctor.ORDER_STATUS_ID_WAIT_ACCEPT))status=OrderDoctor.ORDER_STATUS_ID_ACCEPTED;
        if (st.equals(OrderDoctor.ORDER_STATUS_ID_ACCEPTED))status=OrderDoctor.ORDER_STATUS_ID_WAIT_CONFIRM_DETAILS;
        if (st.equals(OrderDoctor.ORDER_STATUS_ID_WAIT_CONFIRM_DETAILS))status=OrderDoctor.ORDER_STATUS_ID_SERVICING;
        if (st.equals(OrderDoctor.ORDER_STATUS_ID_SERVICING))status=OrderDoctor.ORDER_STATUS_ID_WAIT_CONFIRM_SERVICE;
        if (st.equals(OrderDoctor.ORDER_STATUS_ID_WAIT_CONFIRM_SERVICE))status=OrderDoctor.ORDER_STATUS_ID_SERVICE_DONE;
        if (st.equals(OrderDoctor.ORDER_STATUS_ID_SERVICE_DONE))status=OrderDoctor.ORDER_STATUS_ID_SERVICE_DONE;
        return status;
    }

    public void editOrderBySql(String sql,Object[] paras ){
        int affectRow=0;
        try {
            affectRow = template.update(sql, paras);
        } catch(Exception e) {
            e.printStackTrace();
            throw new RuntimeException("操作失败，请联系系统管理员！");
        }
        if(affectRow == 0) {
            throw new RuntimeException("订单记录不存在！");
        } else if(affectRow > 1) {
            throw new RuntimeException("存在多条订单记录，修改失败，请联系系统管理员！");
        }
    }

    /**
     * 查询邮件数据
     * 返回string数组
     * @param typeId
     * @return
     */
    public String[] findEmail(String typeId){
        String sql="SELECT t.* from tuser t,t_massage_user utm,t_massage tm where t.ID=utm.user_id and tm.id=utm.massage_id and tm.id=?";
        final List<String> list=new ArrayList<>();
        template.query(sql, new Object[]{typeId}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                String s=rs.getString("t.email");
                list.add(s);
            }
        });
        int size=list.size();
        String str[]=(String[])list.toArray(new String[size]);
        return str;
    }
}
